package ai.peanut.one.service.impl;

import ai.peanut.one.ActivityParam;
import ai.peanut.one.common.exception.TestException;
import ai.peanut.one.dao.ActivityMapper;
import ai.peanut.one.entity.Activity;
import ai.peanut.one.service.IActivityService;
import ai.peanut.one.service.ITestService;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lichong
 * @since 2018-11-09
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {

    @Autowired
    private ITestService iTestService ;
    @Override
    public Page<Activity> selectActivity(Page<Activity> page, ActivityParam activityParam) {
        List<Activity> list =  baseMapper.selectActivity( page , activityParam ) ;
        return page.setRecords( list ) ;
    }

    @Override
    public List<Activity> selectActivity(ActivityParam activityParam) {
        return baseMapper.selectActivity(activityParam) ;
    }

    @Transactional
    @Override
    public void a(){
        baseMapper.insert(this.gen("a"));
    }

    @Transactional
    @Override
    public void b1(){
        baseMapper.insert(this.gen("b1"));
    }
    @Override
    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
    public void b2(){
        baseMapper.insert(this.gen("b2"));
        throw new RuntimeException() ;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW, isolation = Isolation.DEFAULT)
    public void b3() {
        baseMapper.insert(this.gen("b3"));
        throw new RuntimeException() ;
    }

    @Override
    public void testC(){
        this.a();
        this.b1();
        throw  new RuntimeException() ;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)

    public void testCc() {

            a();


            b3();
            throw new RuntimeException() ;

    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)

    public void testCcc() {

        a();
        List<Activity> list = baseMapper.selectList(null) ;
        Activity ac = baseMapper.selectById(130);




        iTestService.HaveTExe();
        throw new RuntimeException() ;


    }

    private Activity gen(String name){
        Activity activity = new Activity() ;
        activity.setName(name);
        return activity ;

    }
}
